<?php
/**
 * 
 * 参数
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-04-10
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/

class Param extends CActiveRecord {
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{param}}';
	}
/**
	 * 分页+搜索
	 */
	public function pageSearch($search_info){
		$_POST =$search_info;
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{param}}  WHERE parent_id = 0 AND 1=1 ";
		$nextpage = "";
		if ($_POST){    
			if($_POST['active'] != 3){
				$nextpage .= "&active=".$_POST['active'];
				$_sql.=" AND active=".$_POST['active'];
			}
			if($_POST['keyword'] != null){
				$nextpage .= "&keyword=".$_POST['keyword'];
				$_sql.=" AND  param_name like '%".$_POST['keyword']."%'";
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
			
			/**
			 * 分页
			 */
			$per = 10;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per,$nextpage);
			$_sql .= " ORDER BY param_sort ASC $page->limit";
			$infos = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
			return $infos;
	}
	
	/**
	 * 根据id查询单条记录
	 */
	public function getParamById($param_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{param}} WHERE param_id=".$param_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	/**
	 * 查询所有父pages
	 */
	public function allParent(){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{param}} WHERE parent_id=0 ORDER BY param_sort ASC";
		$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		/**
			 * 分页
			 */
			$per = 10;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per);
			$_sql = "SELECT * FROM {{param}} WHERE parent_id=0 ORDER BY param_sort ASC $page->limit";
			$infos = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
		
		
		$this->infos_count = $cmodel->sel($_sql_count);
		$result = $cmodel->sel($_sql);
		return $result;
	}
	/**
	 * 查询父类对应的子类属性值
	 */
	public function getParentValue($parent_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{param}} WHERE parent_id=".$parent_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/*
	 * 父级属性增加
	 */
	public function addParam($addInfo){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{param}}(
							`parent_id`,
							`param_name`,
							`param_sort`,
							`active`
						) VALUES(
							 0,
							 '".$addInfo['param_name']."',
							 '".$addInfo['param_sort']."',
							 '".$addInfo['active']."'
							 )";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/*
	 *子参数值增加
	 */
	public function addParamValue($parent_id,$addInfo){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{param}}(
							`parent_id`,
							`param_name`,
							`param_sort`,
							`active`
						) VALUES(
							 $parent_id,
							 '".$addInfo['param_name']."',
							 '".$addInfo['param_sort']."',
							 '".$addInfo['active']."'
							 )";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	/**
	 * 修改属性
	 */
	public function updateParam($param_id,$updInfo){
		$cmodel = new CCCModel();
		$_sql = "UPDATE  {{param}} SET
							`param_name` = '".$updInfo['param_name']."',
							`param_sort` = ".$updInfo['param_sort'].", 
							`active` = ".$updInfo['active']."
						WHERE param_id=".$param_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	
	/**
	 * 检测是否已经存在
	 */
	public function checkExist($addInfo){
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT * FROM {{param}} where param_name ='".$addInfo['param_name']."'");
		return $result;
	}
		/**
		 * 删除属性
		 */
		public function paramdel($param_id){
			
			$cmodel = new CCCModel();
			$_sqlinfo = "SELECT parent_id FROM {{param}} WHERE param_id=".$param_id;
			$infos = $cmodel->sel($_sqlinfo);
			if($infos[0]['parent_id'] != 0){
				$_sql = "DELETE FROM {{param}} WHERE param_id=".$param_id;
			}else{
				$_sql = "DELETE FROM {{param}} WHERE param_id=$param_id OR parent_id=".$param_id;
			}
			
			
			$result = $cmodel -> upd($_sql);
			return $result;
		}
}
	
?>